﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{

    /// <summary>
    /// 弹出窗口类
    /// </summary>
    public class Message
    {

        /// <summary>
        /// 页面重载
        /// </summary>
        public static void Location()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("window.location.href=window.location.href;");
            sb.Append("</script>");
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
        }
        /// <summary>
        /// 显示一个弹出窗口
        /// </summary>
        /// <param name="str"></param>
        public static void Show(System.Web.UI.Page page, string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + RemoveEnter(str) + "\"); \n");
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "", sb.ToString());
        }
   
        /// <summary>
        /// 显示一个弹出窗口，并关闭当前页
        /// </summary>
        /// <param name="str"></param>
        public static void ShowClose(string str)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script language=\"javascript\">\n");
            sb.Append("alert(\"" + RemoveEnter(str) + "\"); \n");
            sb.Append("window.close();\n");
            sb.Append("</script>\n");
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
        }

        /// <summary>
        /// 显示一个弹出窗口，并转向当前页(刷新)
        /// </summary>
        /// <param name="str"></param>
        public static void ShowLocation(string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + RemoveEnter(str) + "\"); \n");
            sb.Append("window.location.href=window.location.href;\n");
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 显示一个弹出窗口，并转向目标页(导航)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="url"></param>
        public static void ShowRedirect(string str, string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + RemoveEnter(str) + "\"); \n");
            sb.Append("window.location.href=\"" + url.Trim() + "\";\n");
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 显示一个弹出窗口，并转向上一页
        /// </summary>
        /// <param name="str"></param>
        public static void ShowPre(string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + RemoveEnter(str) + "\"); \n");
            sb.Append("var p=document.referrer; \n");
            sb.Append("window.location.href=p;\n");
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

        public static void ShowBack(string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("alert(\"" + RemoveEnter(str) + "\"); \n");
            sb.Append("history.back();\n");
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 重定向
        /// </summary>
        /// <param name="url"></param>
        public static void RedirectPage(string url)
        {
            string path = url;
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append(string.Format("window.location.href='{0}';", @path));
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

        public static void ShowRedirect(string sec, object reurl, string msg)
        {
            if (reurl == null)
            { reurl = "/"; }
            string str;
            str = "<html><head><meta http-equiv=Content-Type content='text/html; charset=gb2312'>\n";
            str += "<META NAME=\"GOOGLEBOT\" CONTENT=\"NOINDEX, NOFOLLOW\">";
            str += "<meta http-equiv=refresh content='" + sec + "; url=" + reurl + "'>";
            str += "<title>信息提示：(" + sec + "秒后自动返回)</title>";
            str += "</head><body>";
            str += "<table width=\"400\" height=\"119\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"#3366CC\" style=\"font-size: 14.8px;\">";
            str += "<tr><td height=\"27\" bgcolor=\"#999999\"><font color=\"#FFFFFF\"><b>信息提示：(" + sec + "秒后自动返回)</b></font></td>";
            str += "  </tr>";
            str += "  <tr>";
            str += "    <td align=\"center\" bgcolor=\"#ffffff\">" + msg + "<br><a href='" + reurl + "'>如果不想等待请点这里返回!</a></td>";
            str += "  </tr>";
            str += "</table></body></html>";
            System.Web.HttpContext.Current.Response.Write(str);
            System.Web.HttpContext.Current.Response.End();
        }

        private static string RemoveEnter(string s)
        {
            if (string.IsNullOrEmpty(s)) return "";
            return s.Replace("\r\n", "\\n").Replace("\r", "\\n").Replace("\n", "\\n").Trim();
        }
    }
}
